/*
 * ptrvector.hpp
 *
 *  Created on: 2018-7-25
 *      Author: work
 */

#ifndef DM_PTRVECTOR_H_
#define DM_PTRVECTOR_H_

#include <vector>

namespace dm {

template<typename T>
class TCPtrVector {
    TCPtrVector( const TCPtrVector& p );
    TCPtrVector& operator=( const TCPtrVector& p );
public:
    TCPtrVector();
    ~TCPtrVector();

    inline unsigned int size()const{
        return m_e.size();
    }

    T* element( int idx ){
        if( idx<0 || idx>m_e.size() )
            return NULL;
        return m_e[idx];
    }

    const T* element( int idx )const{
        if( idx<0 || ((unsigned int)idx)>m_e.size() )
            return NULL;
        return m_e[idx];
    }

    void append( T*e ){
        m_e.push_back(e);
    }

    void remove( int idx ){
        if( idx>=0 && ((unsigned int)idx)<m_e.size() ){
            delete m_e[idx];
            m_e.erase(m_e.begin()+idx);
        }
    }

    void clear(){
        for( unsigned int i=0;i<m_e.size();++i ){
            if( m_e[i] )
                delete m_e[i];
            m_e.clear();
        }
    }

private:
    std::vector<T*> m_e;
};

} /* namespace Dm */
#endif /* POS_H_ */
